import 'package:flutter/material.dart';
import 'package:hao/page/home/AddRecordPage.dart';
import 'package:hao/page/home/HomePage.dart';
import 'package:hao/page/home/HomeViewModel.dart';
import 'package:hao/page/launch/LaunchPage.dart';
import 'package:hao/page/set/SetPage.dart';
import 'package:hao/utils/ColorUtil.dart';
import 'package:hao/utils/ThemeUtil.dart';
import 'package:get/get.dart';
import 'package:hao/utils/Utils.dart';
import 'package:timezone/data/latest_all.dart' as tz;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  tz.initializeTimeZones();
  await configMode();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: '掌上租管家',
      debugShowCheckedModeBanner: false,
      theme: TTheme.nightTheme,
      themeMode: ThemeMode.dark,
      darkTheme: TTheme.nightTheme,
      home: const TabPage(),
    );
  }
}

class TabPage extends StatefulWidget {
  const TabPage({Key? key}) : super(key: key);

  @override
  State<TabPage> createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
  int lastTime = 0;
  late PageController pageController;
  int page = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        children: [HomePage(), SetPage()],
        onPageChanged: onPageChanged,
        controller: pageController,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Get.to(() => AddRecordPage())?.then((value) {
            if (value != null) {
              HomeViewModel _viewModel = Get.find();
              _viewModel.loadData();
            }
          });
        },
        child: const Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        shape: const CircularNotchedRectangle(),
        child: Padding(
          padding: const EdgeInsets.fromLTRB(0, 6, 0, 6),
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              GestureDetector(
                  onTap: () {
                    onTap(0);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(Icons.home, color: getColor(0)),
                      Text("首页", style: TextStyle(color: getColor(0)))
                    ],
                  )),
              Column(
                mainAxisSize: MainAxisSize.min,
                children: const [
                  Icon(
                    Icons.add,
                    color: Colors.transparent,
                  ),
                ],
              ),
              GestureDetector(
                  onTap: () {
                    onTap(1);
                  },
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Icon(Icons.settings, color: getColor(1)),
                      Text("设置", style: TextStyle(color: getColor(1)))
                    ],
                  )),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: page);
  }

  void onPageChanged(int value) {
    setState(() {
      page = value;
    });
  }

  Color getColor(int value) {
    return page == value
        ? Get.theme.primaryColorDark
        : Colors.grey.withOpacity(0.5);
  }

  void onTap(int value) {
    pageController.animateToPage(value,
        duration: const Duration(milliseconds: 100), curve: Curves.ease);
  }
}
